package com.czkevin.biliwsclient.lottery;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czkevin.biliwsclient.BiliSession;
import com.czkevin.biliwsclient.HTTPHelper;

import java.util.HashMap;
import java.util.Map;

public class LotteryManager {

  public static final String API_ENDPOINT_ADD = "https://api.live.bilibili.com/xlive/lottery-interface/v1/Anchor/Add";
  public static final String API_ENDPOINT_START = "https://api.live.bilibili.com/xlive/lottery-interface/v1/Anchor/Start";

  public static final int DURATION_5_MINUTES = 300;
  public static final int DURATION_10_MINUTES = 600;

  public final BiliSession session;

  public final int room;

  public LotteryManager(BiliSession session, int room) {
    this.session = session;
    this.room = room;
  }

  public int addDanmu(String awardName, int awardNum, String danmuToSend, int duration) throws Exception {
    return add(awardName, awardNum, danmuToSend, duration, JoinCondition.DANMU_ONLY, RequireCondition.FOLLOW);
  }

  public int addGift(String awardName, int awardNum, String danmuToSend, int duration, int giftId, int giftAmount) throws Exception {
    return add(awardName, awardNum, danmuToSend, duration, new JoinCondition(giftId, giftAmount), RequireCondition.FOLLOW);
  }

  public int add(String awardName, int awardNum, String danmuToSend, int duration, JoinCondition joinCondition, RequireCondition requireCondition) throws Exception {
    if(duration != DURATION_5_MINUTES && duration != DURATION_10_MINUTES) {
      throw new IllegalArgumentException("invalid duration");
    }
    final Map<String, String> params = new HashMap<>();
    params.put("award_name", awardName);
    params.put("award_num", Integer.toString(awardNum));
    params.put("danmu", danmuToSend);
    params.put("duration", Integer.toString(duration));
    joinCondition.putParams(params);
    requireCondition.putParams(params);
    params.put("roomid", Integer.toString(room));
    params.put("quiz_roomid", "0");
    params.put("other_follow_roomid", "0");
    JSONObject ret = JSON.parseObject(HTTPHelper.doPostForm(API_ENDPOINT_ADD, params, room, session, null));
    if(ret.getIntValue("code") != 0) {
      throw new IllegalStateException("api returned " + ret.getIntValue("code") + " " + ret.getString("message"));
    }
    JSONObject data = ret.getJSONObject("data");
    if(data.getIntValue("code") != 0) {
      throw new IllegalStateException("add lottery failed, data code: " + data.getIntValue("code"));
    }
    return data.getIntValue("id");
  }

  public void start(int id) throws Exception {
    final Map<String, String> params = new HashMap<>();
    params.put("id", Integer.toString(id));
    params.put("uid", "0");
    JSONObject ret = JSON.parseObject(HTTPHelper.doPostForm(API_ENDPOINT_START, params, room, session, null));
    if(ret.getIntValue("code") != 0) {
      throw new IllegalStateException("api returned " + ret.getIntValue("code") + " " + ret.getString("message"));
    }
  }

}
